#include "gameroom.h"
#include "gamehall.h"
#include <QMessageBox>

GameRoom::GameRoom(QWidget *parent) : QWidget(parent)
{
    this->setFixedSize(1200,800);
    this->setWindowIcon(QIcon(":res/ico.png"));
    this->setWindowTitle("贪吃蛇大作战");

    //初始化 创建蛇
    snakeList.push_back(QRectF(this->width() * 0.5,this->height() * 0.5,ksnakeNodeWidth,ksnakeNodeHeigth));
    moveUp();
    moveUp();

    //创建 食物
    creatNewFood();
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        int cnt = 1;
        if(snakeList.front().intersects(foodNode))
        {
            creatNewFood();
            cnt++;
        }

        while(cnt--)
        {
            switch(moveDirect){
            case snakeDirect::UP:
                moveUp();
                break;
            case snakeDirect::DOWN:
                moveDown();
                break;
            case snakeDirect::LEFT:
                moveLeft();
                break;
            case snakeDirect::RIGHT:
                moveRight();
                break;
            }
        }

        snakeList.pop_back();//???
        update();
    });

    //开始游戏 暂停游戏（ 重新开始？）
    QFont font("华文行楷",20);
    QPushButton *strBtn = new QPushButton(this);
    strBtn->move(1025,95);
    strBtn->setFont(font);
    strBtn->setText("开始游戏");
    connect(strBtn,&QPushButton::clicked,[=](){
        //开始游戏
        isGameStart = true;

        timer->start(kDefaultTimeout);

        sound  = new QSound(":res/Trepak.wav");
        sound->play();
        sound->setLoops(-1);//循环播放

    });

    QPushButton *stopBtn = new QPushButton(this);
    stopBtn->move(1025,150);
    stopBtn->setText("暂停游戏");
    stopBtn->setFont(font);
    connect(stopBtn,&QPushButton::clicked,[=](){
        //暂停游戏
        isGameStart = false;
        sound->stop();
        timer->stop();

    });

    //操控键
    QPushButton *up = new QPushButton(this);
    QPushButton *down = new QPushButton(this);
    QPushButton *left = new QPushButton(this);
    QPushButton *right = new QPushButton(this);

    up->move(1075,350);
    down->move(1075,450);
    left->move(1025,400);
    right->move(1125,400);

    up->setText("↑");
    down->setText("↓");
    left->setText("←");
    right->setText("→");

    QFont dfont("华文行楷",30);
    up->setFont(dfont);
    down->setFont(dfont);
    left->setFont(dfont);
    right->setFont(dfont);

    up->setStyleSheet("QPushButton{border:0px;}");
    down->setStyleSheet("QPushButton{border:0px;}");
    left->setStyleSheet("QPushButton{border:0px;}");
    right->setStyleSheet("QPushButton{border:0px;}");

    connect(up,&QPushButton::clicked,[=](){
        if(moveDirect != snakeDirect::DOWN)
            moveDirect = snakeDirect::UP;
    });

    connect(down,&QPushButton::clicked,[=](){
        if(moveDirect != snakeDirect::UP)
            moveDirect = snakeDirect::DOWN;
    });

    connect(left,&QPushButton::clicked,[=](){
        if(moveDirect != snakeDirect::RIGHT)
            moveDirect = snakeDirect::LEFT;
    });

    connect(right,&QPushButton::clicked,[=](){
        if(moveDirect != snakeDirect::LEFT)
            moveDirect = snakeDirect::RIGHT;
    });

    //退出游戏 or 返回上一界面
    QPushButton *exitBtn = new QPushButton(this);
    exitBtn->move(1025,650);
    exitBtn->setText("退出游戏");
    exitBtn->setFont(font);

    QMessageBox *msg = new QMessageBox(this);
    QPushButton *ok = new QPushButton("ok");
    QPushButton *cancel = new QPushButton("cancel");
    msg->addButton(ok,QMessageBox::AcceptRole);
    msg->addButton(cancel,QMessageBox::RejectRole);

    msg->setWindowTitle("退出游戏");
    msg->setText("确定退出游戏吗?");

    connect(exitBtn,&QPushButton::clicked,[=](){
        msg->show();
        msg->exec();//时间轮询
        QSound::play(":res/clicked.wav");
        if(msg->clickedButton() == ok)
        {
            this->close();
        }
        else
        {
            msg->close();
        }
    });
}

void GameRoom::paintEvent(QPaintEvent *event)
{
    //绘制
    QPainter painter(this);
    QPixmap pix;

    //左侧草地
    pix.load(":res/game_room.png");
    painter.drawPixmap(0,0,1000,this->height(),pix);

    //右侧控制栏
    pix.load(":res/bg1.png");
    painter.drawPixmap(1000,0,200,this->height()+100,pix);

    //分数显示
    pix.load(":res/sorce_bg.png");
    painter.drawPixmap(1050,30,100,40,pix);

    //绘制蛇  蛇头 蛇身 蛇尾
    if(moveDirect == snakeDirect::UP)
    {
        pix.load(":res/up.png");
    }
    else if(moveDirect == snakeDirect::DOWN)
    {
        pix.load(":res/down.png");
    }
    else if(moveDirect == snakeDirect::LEFT)
    {
        pix.load(":res/left.png");
    }
    else if(moveDirect == snakeDirect::RIGHT)
    {
        pix.load(":res/right.png");
    }

    //蛇头结点作为起始的位置
    auto snakeNode = snakeList.front();
    painter.drawPixmap(snakeNode.x(),snakeNode.y(),snakeNode.width(),snakeNode.height(),pix);

    //蛇身
    pix.load(":res/Bd.png");
    for(int i = 1; i < snakeList.size()-1; i++)
    {
        auto node = snakeList.at(i);
        painter.drawPixmap(node.x(),node.y(),node.width(),node.height(),pix);
    }

    //蛇尾
    auto snakeTail = snakeList.back();
    painter.drawPixmap(snakeTail.x(),snakeTail.y(),snakeTail.width(),snakeTail.height(),pix);

    //食物
    pix.load(":res/food.bmp");
    painter.drawPixmap(foodNode.x(),foodNode.y(),foodNode.width(),foodNode.height(),pix);
}

void GameRoom::creatNewFood()
{
    foodNode = QRectF(qrand() % (1000 / ksnakeNodeWidth) * ksnakeNodeWidth,
                      qrand() % (1000 / ksnakeNodeHeigth) * ksnakeNodeHeigth,
                      ksnakeNodeWidth,ksnakeNodeHeigth);
}


bool GameRoom::isGameFail()
{
    for(int i = 0; i < snakeList.size(); i++)
    {
        for(int j = 1; j < snakeList.size(); j++)
        {
            //自己撞到自己   游戏结束
            if(snakeList.at(i) == snakeList.at(j))
                return true;
        }
    }
    return false;
}

void GameRoom::moveUp()
{
    //向上移动
    //寻找 头结点 点位置
    QPointF leftTop;
    QPointF rightBottom;
    auto snakeNode = snakeList.front();
    int headX = snakeNode.x();
    int headY = snakeNode.y();

    if(headY < 0)
    {
        leftTop = QPointF(headX,this->height() - ksnakeNodeHeigth);
    }
    else
    {
        leftTop = QPointF(headX,headY - ksnakeNodeHeigth);
    }
    rightBottom = QPointF(headX + ksnakeNodeWidth,headY + ksnakeNodeHeigth);

    //更新 新的 头结点
    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void GameRoom::moveDown()
{
    //向下移动
    //寻找 头结点 点位置
    QPointF leftTop;
    QPointF rightBottom;
    auto snakeNode = snakeList.front();
    int headX = snakeNode.x();
    int headY = snakeNode.y();

    if(headY > this->height())
    {
        leftTop = QPointF(headX,0);
    }
    else
    {
        leftTop = QPointF(headX,headY + ksnakeNodeHeigth);
    }
    rightBottom = QPointF(headX + ksnakeNodeWidth,headY + ksnakeNodeHeigth);

    //更新 新的 头结点
    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void GameRoom::moveLeft()
{
    //向左移动
    //寻找 头结点 点位置
    QPointF leftTop;
    QPointF rightBottom;
    auto snakeNode = snakeList.front();
    int headX = snakeNode.x();
    int headY = snakeNode.y();

    if(headX < 0)
    {
        leftTop = QPointF(this->width() - ksnakeNodeWidth,headY);
    }
    else
    {
        leftTop = QPointF(headX - ksnakeNodeWidth,headY);
    }
    rightBottom = QPointF(headX + ksnakeNodeWidth,headY + ksnakeNodeHeigth);

    //更新 新的 头结点
    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void GameRoom::moveRight()
{
    //向右移动
    //寻找 头结点 点位置
    QPointF leftTop;
    QPointF rightBottom;
    auto snakeNode = snakeList.front();
    int headX = snakeNode.x();
    int headY = snakeNode.y();

    if(headX > 1000)
    {
        leftTop = QPointF(0,headY);
    }
    else
    {
        leftTop = QPointF(headX + ksnakeNodeWidth,headY);
    }
    rightBottom = QPointF(headX + ksnakeNodeWidth,headY + ksnakeNodeHeigth);

    //更新 新的 头结点
    snakeList.push_front(QRectF(leftTop,rightBottom));
}

